package com.zy.design.pattern.observer.syn;

import com.google.common.collect.Lists;
import com.zy.design.pattern.observer.face.IObservable;
import com.zy.design.pattern.observer.face.IObserver;
import com.zy.design.pattern.observer.face.Message;

import java.util.List;

/**
 * @author zhaoyi
 * @date 2021/4/25 14:23
 * 最简答的一个实现
 */
public class SimpleObservable implements IObservable {

    private List<IObserver> observerList = Lists.newCopyOnWriteArrayList();

    @Override
    public void add(IObserver observer) {
        observerList.add(observer);
    }

    @Override
    public void remove(IObserver observer) {
        observerList.remove(observer);
    }

    @Override
    public void notifyObservers(Message message) {
        for (IObserver observer : observerList) {
            observer.handMessage(message);
        }
    }
}
